

var myApp = angular.module('myApp', []);
  myApp.config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'customer_search.html', controller: 'ListCtrl'}).
      when('/add-newcustomer', {templateUrl: 'add-newcustomer.html', controller: 'AddCtrl'}).
      otherwise({redirectTo: '/'});
}]);


function ListCtrl($scope, $http) {
  $http.get('customerdata.php').success(function(data) {
    $scope.customermast = data;
  });
}


function AddCtrl($scope, $http, $location) {
  $scope.master = {};
  $scope.activePath = null;
  $scope.add_new = function(user, AddNewForm) {
    console.log(user);
    $http.post('add-newcustomer', user).success(function(){
      $scope.reset();
      $scope.activePath = $location.path('/');
    });
    $scope.reset = function() {
      $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
  };
}

//This controller is a child controller that will inherit functionality from a parent
//It's used to track the orderby parameter and ordersTotal for a customer. Put it here rather than duplicating 
//setOrder and orderby across multiple controllers.
//app.controller('OrderChildController', function ($scope) {
//    $scope.orderby = 'product';
//    $scope.reverse = false;
//    $scope.ordersTotal = 0.00;

//    init();

//function init() {
        //Calculate grand total
        //Handled at this level so we don't duplicate it across parent controllers
  //      if ($scope.customer && $scope.customer.orders) {
    //        var total = 0.00;
      ///      for (var i = 0; i < $scope.customer.orders.length; i++) {
         //       var order = $scope.customer.orders[i];
           //     total += order.orderTotal;
        //    }
        //    $scope.ordersTotal = total;
    //    }
//    }

  //  $scope.setOrder = function (orderby) {
    //    if (orderby === $scope.orderby)
      //  {
        //    $scope.reverse = !$scope.reverse;
    //    }
     //   $scope.orderby = orderby;
//    };

//});
